package com.dream.common.utils.payutils;

import com.dream.common.utils.JsonUtils;
import com.dream.common.utils.payutils.bean.BeforeRedPackageBean;
import com.dream.common.utils.payutils.bean.WorkedRedPackageBean;
import com.dream.common.utils.payutils.bean.configuration.*;
import com.dream.entity.PickUserInfoBean;

import java.text.DecimalFormat;
import java.util.*;

//接龙頑珐
public class NextSenderUtils {
    public static WorkedRedPackageBean getWorkedRedPackageBean(BeforeRedPackageBean beforeRedPackageBean, NextSenderRuleBean nextSenderRule) {
        WorkedRedPackageBean workedRedPackageBean = new WorkedRedPackageBean();
        workedRedPackageBean.bijiAccount = beforeRedPackageBean.userName;
        workedRedPackageBean.groupName = beforeRedPackageBean.groupName;
        workedRedPackageBean.groupID = beforeRedPackageBean.groupId;
        workedRedPackageBean.sendUserID = beforeRedPackageBean.sendId;
        workedRedPackageBean.sendUsername = beforeRedPackageBean.sendName;
        workedRedPackageBean.sendHeadImage = beforeRedPackageBean.sendHeadImg;
        workedRedPackageBean.redPackageID = beforeRedPackageBean.msgId;
        workedRedPackageBean.sendTime = System.currentTimeMillis();
        workedRedPackageBean.sendMoney = Double.parseDouble(beforeRedPackageBean.resultData.totalfee);
        workedRedPackageBean.sendNumber = beforeRedPackageBean.resultData.totalnum;
        workedRedPackageBean = checkSendMoneyAndPackageNumber(workedRedPackageBean, nextSenderRule);
        if (workedRedPackageBean.isErrorRedPackage) {
            return workedRedPackageBean;
        }
        workedRedPackageBean = setBasePickerInfo(workedRedPackageBean, beforeRedPackageBean.resultData.cxuserredpackagelist);
        workedRedPackageBean = setNoDeadPeople(workedRedPackageBean, nextSenderRule);
        workedRedPackageBean = setPickerRewardInfo(workedRedPackageBean, nextSenderRule);
        workedRedPackageBean = setPickUserEndNumber(workedRedPackageBean, nextSenderRule);
        workedRedPackageBean = setNextSendUserInfo(workedRedPackageBean, nextSenderRule);
        return workedRedPackageBean;
    }

    /**
     * 设置接包玩家信息
     *
     * @param workedRedPackageBean
     * @param nextSenderRule
     * @return
     */

    private static WorkedRedPackageBean setNextSendUserInfo(WorkedRedPackageBean workedRedPackageBean, NextSenderRuleBean nextSenderRule) {
        String nextSendUserId = null;
        List<String> nextSendUserIdList = null;
        switch (nextSenderRule.nextSenderType) {
            case 1://比大小
                nextSendUserId = getNextSendUserIdByMoney(workedRedPackageBean, nextSenderRule);
                break;
            case 2://比尾数
                nextSendUserId = getNextSendUserIdByEndNumber(workedRedPackageBean, nextSenderRule);
                break;
            case 4://双A
                nextSendUserIdList = getNextSendUserIdsByDoubleA(workedRedPackageBean, nextSenderRule);
                break;
            case 3://免死上下级
                nextSendUserId = getNextSendUserIdsByNoDeadUpperAndLower(workedRedPackageBean, nextSenderRule);
                break;
        }
        for (PickUserInfoBean pickUserInfoBean : workedRedPackageBean.pickUserList) {
            if (nextSenderRule.nextSenderType == 4) {
                if (nextSendUserIdList != null && nextSendUserIdList.size() != 0 && nextSendUserIdList.contains(pickUserInfoBean.userID)) {
                    pickUserInfoBean.isNextSendUser = true;
                }
            } else {
                if (pickUserInfoBean.userID.equals(nextSendUserId)) {
                    pickUserInfoBean.isNextSendUser = true;
                }
            }
        }
        return workedRedPackageBean;
    }

    private static String getNextSendUserFloorByNoDead(WorkedRedPackageBean workedRedPackageBean, NextSenderRuleBean nextSenderRule) {
        String nextSendUserId = null;
        List<PickUserInfoBean> pickUserList = workedRedPackageBean.pickUserList;
        switch (nextSenderRule.noDeadType) {
            case 1://免死上级发（比免死大的）
                if (nextSenderRule.noDeadSameMoneyType == 1) {//同琻额楼上发
                    //琻额从大到小排序，同琻额按楼层从小到大排，免死上面的一个就是
                    Collections.sort(pickUserList, (o1, o2) -> {
                        double moneyResult = o2.pickMoney - o1.pickMoney;
                        if (moneyResult == 0) {
                            int floorResult = o1.floor - o2.floor;
                            return floorResult;
                        }
                        if (moneyResult > 0) {
                            return 1;
                        } else {
                            return -1;
                        }
                    });
                } else {//同琻额楼下发
                    //琻额从大到小排序，同琻额按楼层从大到小排，免死上面的一个就是
                    Collections.sort(pickUserList, (o1, o2) -> {
                        double moneyResult = o2.pickMoney - o1.pickMoney;
                        if (moneyResult == 0) {
                            int floorResult = o2.floor - o1.floor;
                            return floorResult;
                        }
                        if (moneyResult > 0) {
                            return 1;
                        } else {
                            return -1;
                        }
                    });
                }
                break;
            case 2://免死下级发（比免死小的）
                if (nextSenderRule.noDeadSameMoneyType == 1) {//同琻额楼上发
                    //琻额从小到大排序，同琻额按楼层从小到大排，免死上面的一个就是
                    Collections.sort(pickUserList, (o1, o2) -> {
                        double moneyResult = o1.pickMoney - o2.pickMoney;
                        if (moneyResult == 0) {
                            int floorResult = o1.floor - o2.floor;
                            return floorResult;
                        }
                        if (moneyResult > 0) {
                            return 1;
                        } else {
                            return -1;
                        }
                    });
                } else {//同琻额楼下发
                    //琻额从小到大排序，同琻额按楼层从大到小排，免死上面的一个就是
                    Collections.sort(pickUserList, (o1, o2) -> {
                        double moneyResult = o1.pickMoney - o2.pickMoney;
                        if (moneyResult == 0) {
                            int floorResult = o2.floor - o1.floor;
                            return floorResult;
                        }
                        if (moneyResult > 0) {
                            return 1;
                        } else {
                            return -1;
                        }
                    });
                }
                break;
        }
        for (int i = 0; i < pickUserList.size(); i++) {
            if (pickUserList.get(i).isNoDead) {
                if (i == 0) {//免死在最上面，取最下面一个
                    nextSendUserId = pickUserList.get(pickUserList.size() - 1).userID;
                } else {
                    nextSendUserId = pickUserList.get(i - 1).userID;
                }
            }
        }
        return nextSendUserId;
    }

    private static String getNextSendUserIdsByNoDeadUpperAndLower(WorkedRedPackageBean workedRedPackageBean, NextSenderRuleBean nextSenderRule) {
        String nextSendUserId = null;
        boolean noDeadIsPick = false;
        if (nextSenderRule.noDeadSwitch && nextSenderRule.noDeadId != null) {
            for (PickUserInfoBean pickUserInfoBean : workedRedPackageBean.pickUserList) {
                if (pickUserInfoBean.userID.equals(nextSenderRule.noDeadId)) {
                    pickUserInfoBean.isNoDead = true;
                    noDeadIsPick = true;
                    break;
                }
            }
        }

        if (noDeadIsPick) {
            nextSendUserId = getNextSendUserFloorByNoDead(workedRedPackageBean, nextSenderRule);
        } else {
            switch (nextSenderRule.noDeadNotPickType) {
                case 1:
                    nextSendUserId = getNextSendUserIdByMoney(workedRedPackageBean, nextSenderRule);
                    break;
                case 2:
                    nextSendUserId = getNextSendUserIdByEndNumber(workedRedPackageBean, nextSenderRule);
                    break;
            }
        }
        return nextSendUserId;
    }

    /**
     * 双A頑珐
     *
     * @param workedRedPackageBean
     * @param nextSenderRule
     * @return
     */
    private static List<String> getNextSendUserIdsByDoubleA(WorkedRedPackageBean workedRedPackageBean, NextSenderRuleBean nextSenderRule) {
        List<PickUserInfoBean> pickUserList = workedRedPackageBean.pickUserList;
        List<PickUserInfoBean> pickUserListNoDead = new ArrayList();
        for (PickUserInfoBean pickUserInfoBean : pickUserList) {
            if (!pickUserInfoBean.isNoDead) {
                pickUserListNoDead.add(pickUserInfoBean);
            }
        }
        List<String> nextSendUserIdList = new ArrayList<>();
        if (nextSenderRule.doubleAType == 1) {//两个最大发
            //三个及以上琻额相同楼下发
            if (nextSenderRule.doubleAThreeSameType == 2) {
                //琻额从大到小排序，同琻额按楼层从大到小排
                Collections.sort(pickUserListNoDead, (o1, o2) -> {
                    double moneyResult = o2.pickMoney - o1.pickMoney;
                    if (moneyResult == 0) {
                        int floorResult = o2.floor - o1.floor;
                        return floorResult;
                    }
                    if (moneyResult > 0) {
                        return 1;
                    } else {
                        return -1;
                    }
                });
            } else { //三个及以上琻额相同楼上发
                //琻额从大到小排序，同琻额按楼层从小到大排
                Collections.sort(pickUserListNoDead, (o1, o2) -> {
                    double moneyResult = o2.pickMoney - o1.pickMoney;
                    if (moneyResult == 0) {
                        int floorResult = o1.floor - o2.floor;
                        return floorResult;
                    }
                    if (moneyResult > 0) {
                        return 1;
                    } else {
                        return -1;
                    }
                });
            }
        } else {//两个最小发
            //三个及以上琻额相同楼下发
            if (nextSenderRule.doubleAThreeSameType == 2) {
                //琻额从小到大排序，同琻额按楼层从大到小排
                Collections.sort(pickUserListNoDead, (o1, o2) -> {
                    double moneyResult = o1.pickMoney - o2.pickMoney;
                    if (moneyResult == 0) {
                        int floorResult = o2.floor - o1.floor;
                        return floorResult;
                    }
                    if (moneyResult > 0) {
                        return 1;
                    } else {
                        return -1;
                    }
                });
            } else { //三个及以上琻额相同楼上发
                //琻额从小到大排序，同琻额按楼层从小到大排
                Collections.sort(pickUserListNoDead, (o1, o2) -> {
                    double moneyResult = o1.pickMoney - o2.pickMoney;
                    if (moneyResult == 0) {
                        int floorResult = o1.floor - o2.floor;
                        return floorResult;
                    }
                    if (moneyResult > 0) {
                        return 1;
                    } else {
                        return -1;
                    }
                });
            }
        }
        nextSendUserIdList.add(pickUserListNoDead.get(0).userID);
        nextSendUserIdList.add(pickUserListNoDead.get(1).userID);
        return nextSendUserIdList;
    }

    private static String getNextSendUserIdByEndNumber(WorkedRedPackageBean workedRedPackageBean, NextSenderRuleBean nextSenderRule) {
        List<PickUserInfoBean> pickUserList = workedRedPackageBean.pickUserList;
        switch (nextSenderRule.endNumberType) {
            case 1://尾数最大发
                switch (nextSenderRule.sameEndNumberType) {
                    case 1://1同尾数楼上发
                        //尾数从大到小排序，同尾数按楼层从小到大排
                        Collections.sort(pickUserList, (o1, o2) -> {
                            double moneyResult = o2.endNumber - o1.endNumber;
                            if (moneyResult == 0) {
                                int floorResult = o1.floor - o2.floor;
                                return floorResult;
                            }
                            if (moneyResult > 0) {
                                return 1;
                            } else {
                                return -1;
                            }
                        });
                        break;
                    case 2://2同尾数楼下发
                        //尾数从大到小排序，同尾数按楼层从大到小排
                        Collections.sort(pickUserList, (o1, o2) -> {
                            double moneyResult = o2.endNumber - o1.endNumber;
                            if (moneyResult == 0) {
                                int floorResult = o2.floor - o1.floor;
                                return floorResult;
                            }
                            if (moneyResult > 0) {
                                return 1;
                            } else {
                                return -1;
                            }
                        });
                        break;
                }
                break;
            case 2://尾数最小发
                switch (nextSenderRule.sameEndNumberType) {
                    case 1://1同尾数楼上发
                        //尾数从小到大排序，同尾数按楼层从小到大排
                        Collections.sort(pickUserList, (o1, o2) -> {
                            double moneyResult = o1.endNumber - o2.endNumber;
                            if (moneyResult == 0) {
                                int floorResult = o1.floor - o2.floor;
                                return floorResult;
                            }
                            if (moneyResult > 0) {
                                return 1;
                            } else {
                                return -1;
                            }
                        });
                        break;
                    case 2://2同尾数楼下发
                        //尾数从小到大排序，同尾数按楼层从大到小排
                        Collections.sort(pickUserList, (o1, o2) -> {
                            double moneyResult = o1.endNumber - o2.endNumber;
                            if (moneyResult == 0) {
                                int floorResult = o2.floor - o1.floor;
                                return floorResult;
                            }
                            if (moneyResult > 0) {
                                return 1;
                            } else {
                                return -1;
                            }
                        });
                        break;
                }
                break;
        }
        if (pickUserList.get(0).isNoDead) {
            return pickUserList.get(1).userID;
        } else {
            return pickUserList.get(0).userID;
        }
    }

    private static String getNextSendUserIdByMoney(WorkedRedPackageBean workedRedPackageBean, NextSenderRuleBean nextSenderRule) {
        List<PickUserInfoBean> pickUserList = workedRedPackageBean.pickUserList;
        switch (nextSenderRule.moneyType) {
            case 1://1琻额最大发
                switch (nextSenderRule.sameMoneyType) {
                    case 1://1同琻额楼上发
                        //琻额从大到小排序，同琻额按楼层从小到大排
                        Collections.sort(pickUserList, (o1, o2) -> {
                            double moneyResult = o2.pickMoney - o1.pickMoney;
                            if (moneyResult == 0) {
                                int floorResult = o1.floor - o2.floor;
                                return floorResult;
                            }
                            if (moneyResult > 0) {
                                return 1;
                            } else {
                                return -1;
                            }
                        });
                        break;
                    case 2://2同琻额楼下发
                        //琻额从大到小排序，同琻额按楼层从大到小排
                        Collections.sort(pickUserList, (o1, o2) -> {
                            double moneyResult = o2.pickMoney - o1.pickMoney;
                            if (moneyResult == 0) {
                                int floorResult = o2.floor - o1.floor;
                                return floorResult;
                            }
                            if (moneyResult > 0) {
                                return 1;
                            } else {
                                return -1;
                            }
                        });
                        break;
                }
                break;
            case 2://2琻额最小发
                switch (nextSenderRule.sameMoneyType) {
                    case 1://1同琻额楼上发
                        //琻额从小到大排序，同琻额按楼层从小到大排
                        Collections.sort(pickUserList, (o1, o2) -> {
                            double moneyResult = o1.pickMoney - o2.pickMoney;
                            if (moneyResult == 0) {
                                int floorResult = o1.floor - o2.floor;
                                return floorResult;
                            }
                            if (moneyResult > 0) {
                                return 1;
                            } else {
                                return -1;
                            }
                        });
                        break;
                    case 2://2同琻额楼下发
                        //琻额从大到小排序，同琻额按楼层从大到小排
                        Collections.sort(pickUserList, (o1, o2) -> {
                            double moneyResult = o1.pickMoney - o1.pickMoney;
                            if (moneyResult == 0) {
                                int floorResult = o2.floor - o1.floor;
                                return floorResult;
                            }
                            if (moneyResult > 0) {
                                return 1;
                            } else {
                                return -1;
                            }
                        });
                        break;
                }
                break;
        }
        if (pickUserList.get(0).isNoDead) {
            return pickUserList.get(1).userID;
        } else {
            return pickUserList.get(0).userID;
        }
    }

    private static WorkedRedPackageBean setPickUserEndNumber(WorkedRedPackageBean workedRedPackageBean, NextSenderRuleBean nextSenderRule) {
        List<PickUserInfoBean> pickUserList = workedRedPackageBean.pickUserList;
        //1收尾相加，2多选相加
        int endThunderType = nextSenderRule.endThunderType;
        for (PickUserInfoBean pickUserInfoBean : pickUserList) {
            String pickMoney = new DecimalFormat("#,##0.00").format(pickUserInfoBean.pickMoney);
            String pickMoneyStr = pickMoney.replace(".", "");
            char[] pickMoneyArr = pickMoneyStr.toCharArray();
            if (pickUserInfoBean.endNumber == null) {
                pickUserInfoBean.endNumber = 0;
            }
            if (endThunderType == 1) {
                pickUserInfoBean.endNumber = Integer.parseInt(String.valueOf(pickMoneyArr[pickMoneyArr.length - 1]))
                        + Integer.parseInt(String.valueOf(pickMoneyArr[0]));
            } else if (endThunderType == 2) {
                final Map<String, Boolean> endNumberMap = nextSenderRule.endNumberMap;
                int bai = pickMoneyArr.length >= 5 ? Integer.parseInt(String.valueOf(pickMoneyArr[0])) : 0;
                int shi = pickMoneyArr.length >= 4 ? Integer.parseInt(String.valueOf(pickMoneyArr[pickMoneyArr.length - 4])) : 0;
                int yuan = Integer.parseInt(String.valueOf(pickMoneyArr[pickMoneyArr.length - 3]));
                int jiao = Integer.parseInt(String.valueOf(pickMoneyArr[pickMoneyArr.length - 2]));
                int fen = Integer.parseInt(String.valueOf(pickMoneyArr[pickMoneyArr.length - 1]));
                if (endNumberMap.get("bai")) {
                    pickUserInfoBean.endNumber = pickUserInfoBean.endNumber + bai;
                }
                if (endNumberMap.get("shi")) {
                    pickUserInfoBean.endNumber = pickUserInfoBean.endNumber + shi;
                }
                if (endNumberMap.get("yuan")) {
                    pickUserInfoBean.endNumber = pickUserInfoBean.endNumber + yuan;
                }
                if (endNumberMap.get("jiao")) {
                    pickUserInfoBean.endNumber = pickUserInfoBean.endNumber + jiao;
                }
                if (endNumberMap.get("fen")) {
                    pickUserInfoBean.endNumber = pickUserInfoBean.endNumber + fen;
                }
            }
        }
        return workedRedPackageBean;
    }

    /**
     * 设置枪包玩家特殊琻额奖励
     *
     * @param workedRedPackageBean
     * @param nextSenderRule
     * @return
     */

    private static WorkedRedPackageBean setPickerRewardInfo(WorkedRedPackageBean workedRedPackageBean, NextSenderRuleBean nextSenderRule) {
        double allRewardsMoney = 0;
        if (!nextSenderRule.specialMoneyRewardSwitch) {
            return workedRedPackageBean;
        }
        List<PickUserInfoBean> pickUserList = workedRedPackageBean.pickUserList;
        Map<String, Double> specialMoneyRewardMap = nextSenderRule.specialMoneyRewardMap;
        final Set<String> pickMoneySet = specialMoneyRewardMap.keySet();
        for (PickUserInfoBean pickUserInfoBean : pickUserList) {
            if (pickMoneySet.contains(pickUserInfoBean.pickMoney + "")) {
                pickUserInfoBean.rewardsMoney = specialMoneyRewardMap.get(pickUserInfoBean.pickMoney + "");
                allRewardsMoney = allRewardsMoney + specialMoneyRewardMap.get(pickUserInfoBean.pickMoney + "");
            }
        }
        workedRedPackageBean.allRewardsMoney = allRewardsMoney;
        return workedRedPackageBean;
    }

    private static WorkedRedPackageBean setBasePickerInfo(WorkedRedPackageBean workedRedPackageBean, List<BeforeRedPackageBean.ResultData.Cxuserredpackagelist> pickUserList) {
        List<PickUserInfoBean> workedPickUserList = new ArrayList<>();
        for (int i = 0; i < pickUserList.size(); i++) {
            BeforeRedPackageBean.ResultData.Cxuserredpackagelist beforePickUser = pickUserList.get(i);
            PickUserInfoBean pickUserInfoBean = new PickUserInfoBean();
            pickUserInfoBean.bijiAccount = workedRedPackageBean.bijiAccount;
            pickUserInfoBean.userID = beforePickUser.taccid;
            pickUserInfoBean.username = beforePickUser.tname;
            pickUserInfoBean.userHeadImage = beforePickUser.ticon;
            pickUserInfoBean.pickTime = beforePickUser.endtime;
            pickUserInfoBean.pickMoney = beforePickUser.money;
            pickUserInfoBean.floor = i + 1;
            workedPickUserList.add(pickUserInfoBean);
        }
        workedRedPackageBean.pickUserList = workedPickUserList;
        return workedRedPackageBean;
    }

    private static WorkedRedPackageBean checkSendMoneyAndPackageNumber(WorkedRedPackageBean workedRedPackageBean, NextSenderRuleBean nextSenderRule) {
        if (nextSenderRule == null) {
            workedRedPackageBean.isErrorRedPackage = true;
            workedRedPackageBean.errorRedPackageInfo = "未获取到接龙頑珐配置"
                    + "\n1，登陆云算法官网"
                    + "\n2，打开賠率设置-接龙頑珐";
            return workedRedPackageBean;
        }
        if (nextSenderRule.sendMoneySwitch && !nextSenderRule.sendMoney.equals(workedRedPackageBean.sendMoney)) {
            workedRedPackageBean.isErrorRedPackage = true;
            workedRedPackageBean.errorRedPackageInfo = "伐包琻额不是指定琻额"
                    + "\n1，登陆云算法官网"
                    + "\n2，打开賠率设置-接龙頑珐"
                    + "\n3，设置指定琻额";
            return workedRedPackageBean;
        }
        if (nextSenderRule.packageNumberSwitch && (nextSenderRule.packageNumberMap == null ||!nextSenderRule.packageNumberMap.get(workedRedPackageBean.sendNumber + ""))) {
            workedRedPackageBean.isErrorRedPackage = true;
            workedRedPackageBean.errorRedPackageInfo = "伐包包数不是指定包数"
                    + "\n1，登陆云算法官网"
                    + "\n2，打开賠率设置-接龙頑珐"
                    + "\n3，设置指定包数";
            return workedRedPackageBean;
        }
        return workedRedPackageBean;
    }

    private static WorkedRedPackageBean setNoDeadPeople(WorkedRedPackageBean workedRedPackageBean, NextSenderRuleBean nextSenderRule) {
        double noDeadPickMoney = 0.0;
        if (nextSenderRule.noDeadSwitch && nextSenderRule.noDeadId != null && !"".equals(nextSenderRule.noDeadId)) {
            List<PickUserInfoBean> pickUserList = workedRedPackageBean.pickUserList;
            for (PickUserInfoBean pickUserInfoBean : pickUserList) {
                if (pickUserInfoBean.userID.equals(nextSenderRule.noDeadId)) {
                    pickUserInfoBean.isNoDead = true;
                    noDeadPickMoney = noDeadPickMoney + pickUserInfoBean.pickMoney;
                }
            }
            workedRedPackageBean.pickUserList = pickUserList;
            workedRedPackageBean.noDeadPickMoney = noDeadPickMoney;
        }
        return workedRedPackageBean;
    }
}
